package java8.stream;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * @author 周宇
 * @create 2020-05-19 22:35
 */
public class StreamAPI {
    public static void main(String[] args) {
        List<Student> studentList = new ArrayList<>();
        studentList.add(new Student("张三", 18, "C2001", '男'));
        studentList.add(new Student("李四", 19, "C2001", '男'));
        studentList.add(new Student("赵丽", 17, "C2002", '女'));
        studentList.add(new Student("王翠", 18, "C2002", '女'));
        studentList.add(new Student("罗平", 20, "C2003", '男'));
        studentList.add(new Student("杨馋", 19, "C2003", '女'));
        studentList.add(new Student("张三", 18, "C2001", '男'));


        // 1）找到年龄大于18岁的人并输出
        studentList.stream().filter((s) -> s.getAge() > 18).forEach(System.out::println);
        System.out.println("-------------------------------------------");
        // 2）所有C2001班的学生数量
        long c2001 = studentList.stream().filter((s) -> s.getGrade().equals("C2001")).count();
        System.out.println("所有C2001班的学生数量:" + c2001);
        System.out.println("-------------------------------------------");

        //limit举例:limit举例：叫两名女学生志愿者​
        studentList.stream().filter((s) -> s.getSex() == '女').limit(2).forEach(System.out::println);
        System.out.println("-------------------------------------------");

        //skip举例:除了前两名志愿者，所有女学生去上课
        studentList.stream().filter(s -> s.getSex() == '女').skip(2).forEach(System.out::println);
        System.out.println("-------------------------------------------");

        //distinct举例：去除所有男同学中重复的人
        studentList.stream().filter(s -> s.getSex() == '男').distinct().forEach(System.out::println);
        System.out.println("-------------------------------------------");

        //使用StudentGrade来接收班级消息
        studentList.stream().map(s -> {
            StudentGrade student = new StudentGrade();
            student.setGrade(s.getGrade());
            return student;
        }).distinct().forEach(System.out::println);

        System.out.println("-------------------------------------------");
        List<Integer> listInteget = Arrays.asList(1, 0, 2, -1, -10, 6, 0, -25, 90);
        //自然排序 从小到大
        listInteget.stream().sorted().forEach(i -> System.out.print(i + ","));
        System.out.println("\n-------------------------------------------");
        //定制排序 从大到小
        listInteget.stream().sorted((o1, o2) -> o2.compareTo(o1)).forEach(i -> System.out.print(i + ","));
        System.out.println("\n-------------------------------------------");

        //allMatch举例：判断学生是否都年满18周岁
        boolean allmatkch = studentList.stream().allMatch(s -> s.getAge() >= 18);
        System.out.println("学生是否都年满18周岁:" + allmatkch);
        //是否都是年级C组学生
        boolean Call = studentList.stream().allMatch(s -> s.getGrade().contains("C"));
        System.out.println("是否都是年级C组学生:" + Call);

        //年龄最大的学生
        Optional<Student> maxStudent = studentList.stream().max((o1, o2) -> o1.getAge().compareTo(o2.getAge()));
        System.out.println("年龄最大的学生" + maxStudent.get());
        //年龄最小的学生
        Optional<Student> minStudent = studentList.stream().min((o1, o2) -> o1.getAge().compareTo(o2.getAge()));
        System.out.println("年龄最小的学生" + minStudent.get());

        System.out.println("\n-------------------------------------------");
        //求一个1到100的和
        List<Integer> integers = new ArrayList<>();
        for (int i = 1; i <= 100; i++) {
            integers.add(i);
        }
        //reduce第二个方法:T reduce(T identity, BinaryOperator<T> accumulator)
        Integer reduce = integers.stream().reduce(0, (x, y) -> x + y);
        System.out.println("求一个1到100的和:" + reduce);//5050
        //求所有学生的年龄之和
        Optional<Integer> reduce1 = studentList.stream().distinct().map(Student::getAge).reduce(Integer::sum);
        System.out.println("所有学生的年龄之和:" + reduce1.get());//111
        System.out.println("\n-------------------------------------------");

        // 将所有年级信息保存到List集合
        List<String> greads = studentList.stream().map(s -> s.getGrade()).distinct().collect(Collectors.toList());
        System.out.println(greads);

        Double collage = studentList.stream().collect(Collectors.averagingInt(s -> s.getAge()));
        System.out.println("平均年龄为：" + collage);
        System.out.println("\n-------------------------------------------");

        //找出最小年龄、最大年龄
        Optional<Integer> maxAge2 = studentList.stream().map(Student::getAge).collect(Collectors.maxBy(Integer::compareTo));
        System.out.println("最大年龄:" + maxAge2.get());
        Optional<Integer> minAge2 = studentList.stream().map(Student::getAge).collect(Collectors.minBy(Integer::compareTo));
        System.out.println("最小年龄:" + minAge2.get());

        //放到try-with-resources中
        try (Stream<Integer> integerStream = studentList.stream().map(Student::getAge)) {
            Optional<Integer> minAge = integerStream.collect(Collectors.minBy(Integer::compareTo));
            System.out.println(minAge.get());
        }
    }
}
